package controllers

import (
    "encoding/json"
    "github.com/beego/beego/v2/server/web"
    "strings"
    "volux/utils"
)

type TransportController struct {
    web.Controller
}

type Transport struct {
    Type          string `json:"type"`
    MaxQueueDepth uint32 `json:"max_queue_depth"`
    MaxIOQPairs   uint32 `json:"max_io_qpairs"`
}

func (c *TransportController) CreateTransport() {
    var transport struct {
        Trtype              string `json:"trtype"`
        MaxQueueDepth       int    `json:"max_queue_depth"`
        MaxIoQpairsPerCtrlr int    `json:"max_io_qpairs_per_ctrlr"`
        InCapsuleDataSize   int    `json:"in_capsule_data_size"`
        MaxIoSize           int    `json:"max_io_size"`
        IoUnitSize          int    `json:"io_unit_size"`
        MaxAqDepth          int    `json:"max_aq_depth"`
    }

    // 使用 json.Unmarshal 替代 ParseForm
    if err := json.Unmarshal(c.Ctx.Input.RequestBody, &transport); err != nil {
        utils.ErrorResponse(c.Controller, 400, "Invalid request body")
        return
    }

    // 验证必填参数
    if transport.Trtype == "" {
        utils.ErrorResponse(c.Controller, 400, "Transport type (trtype) is required")
        return
    }

    // 验证 trtype 值，改为不区分大小写的比较
    trtype := strings.ToUpper(transport.Trtype)
    if trtype != "TCP" && trtype != "RDMA" && trtype != "FC" {
        utils.ErrorResponse(c.Controller, 400, "Invalid transport type. Must be TCP, RDMA or FC")
        return
    }

    // 调用 SPDK 创建 transport
    spdkClient := utils.GetSPDKClient()
    if spdkClient == nil {
        utils.ErrorResponse(c.Controller, 500, "SPDK client not available")
        return
    }

    // 构造 SPDK 调用参数
    params := map[string]interface{}{
        "trtype":              trtype,
        "max_queue_depth":     transport.MaxQueueDepth,
        "max_io_qpairs":       transport.MaxIoQpairsPerCtrlr,
        "in_capsule_data_size": transport.InCapsuleDataSize,
        "max_io_size":         transport.MaxIoSize,
        "io_unit_size":        transport.IoUnitSize,
        "max_aq_depth":        transport.MaxAqDepth,
    }

    resp, err := spdkClient.Call("nvmf_create_transport", params)
    if err != nil {
        utils.ErrorResponse(c.Controller, 500, "Failed to create transport: "+err.Error())
        return
    }

    utils.SuccessResponse(c.Controller, resp)
}

// GetTransports 获取所有传输层配置
func (c *TransportController) GetTransports() {
    spdkClient := utils.GetSPDKClient()
    if spdkClient == nil {
        utils.ErrorResponse(c.Controller, 500, "SPDK client not available")
        return
    }

    transports, err := spdkClient.GetTransports()
    if err != nil {
        utils.ErrorResponse(c.Controller, 500, "Failed to get transports")
        return
    }

    utils.SuccessResponse(c.Controller, transports)
}

// UpdateTransport 更新传输层配置
func (c *TransportController) UpdateTransport() {
    var transport Transport
    if err := json.Unmarshal(c.Ctx.Input.RequestBody, &transport); err != nil {
        utils.ErrorResponse(c.Controller, 400, "Invalid request body")
        return
    }

    spdkClient := utils.GetSPDKClient()
    if spdkClient == nil {
        utils.ErrorResponse(c.Controller, 500, "SPDK client not available")
        return
    }

    _, err := spdkClient.Call("nvmf_transport_set_config", map[string]interface{}{
        "trtype": transport.Type,
        "max_queue_depth": transport.MaxQueueDepth,
        "max_io_qpairs": transport.MaxIOQPairs,
    })

    if err != nil {
        utils.ErrorResponse(c.Controller, 500, "Failed to update transport")
        return
    }

    utils.SuccessResponse(c.Controller, transport)
}
